﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HelloWPF.ObjectPpropertyAssignment
{
    /// <summary>
    /// ObjectPproperty1.xaml 的交互逻辑
    /// </summary>
    public partial class AttributeValue : Window
    {
        public AttributeValue()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Human currentHuman = this.FindResource("human") as Human;
            if (currentHuman != null)
            {
                MessageBox.Show(currentHuman.Child.Name);
            }
        }
    }

    /// <summary>
    /// NameToHumanTypeConverter这个类附加到Human类上面
    /// </summary>
    [TypeConverterAttribute(typeof(NameToHumanTypeConverter))]
    public class Human
    {
        public string Name { get; set; }
        public Human Child { get; set; }
    }

    /// <summary>
    /// NameToHumanTypeConverter继承TypeConverter复写ConvertFrom方法
    /// </summary>
    public class NameToHumanTypeConverter : TypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string name = value.ToString();
            Human child = new Human();
            child.Name = name;

            return child;
        }
    }
}
